package com.hns.can.service.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hns.can.common.web.BaseController;
import com.hns.can.service.entity.AppCarOnlineDetailEntity;
import com.hns.can.service.entity.BsDayCarOnlineInfoEntity;
import com.hns.can.service.service.DeviceOnlineService;
import com.hns.can.service.vo.DeviceChartVo;
import com.hns.tool.export.ExportUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @版权：HNS 版权所有 (c) 2013
 * @author:zhangy
 * @version Revision 1.0.0
 * @email:zhangyu2222z@sina.cn
 * @see:
 * @创建日期：2015年7月27日
 * @功能说明：设备在线controller
 * @begin
 * @修改记录:
 * @修改后版本          修改人      	修改内容
 * @2015年7月27日  	       zhangy       	创建
 * @end
 */
@Controller
@RequestMapping("/deviceOnline")
public class DeviceOnlineController  extends BaseController {

    @Autowired
    private DeviceOnlineService deviceOnlineService;

	/**
	 * 导出文件表头
	 */
	private final String[] tableHeaderNames={"数据日期","车牌号","运营线路","设备ID","SIM卡号","最早上线时间","最后下线时间","总时长","下线次数","下线时长","离线率"};
	/**
	 * 导出文件字段名
	 */
	private final String[] tableHeaderFields={"rcrdDate","licPltNo","lineName","eqmtCd","simCardno","minRcrdTime","maxRcrdTime","totalTime","offlineCount","offlineTime","offlineRate"};

    /**
     * 导出文件表头
     */
    private final String[] tableHeaderNames2={"车牌号","记录日期","在线状态"};
    /**
     * 导出文件字段名
     */
    private final String[] tableHeaderFields2={"licPltNo","rcrdDate","onlineSt"};
	
    /**
     * @param vo
     * @throws Exception
     * @author:zhangy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2015年7月27日
     * @功能说明：查询设备在线
     */
	@RequestMapping("/getDeviceOnlineStatus")
	@ResponseBody
	public Object getDeviceOnlineStatus(CommonParamsVo vo) throws Exception {
		if(PubUtil.isNotEmpty(vo.getCarIds())){
			List<String> orgIds = Arrays.asList(vo.getCarIds());
			if(PubUtil.isNotEmpty(orgIds)){
				vo.setOrgIds(orgIds);
			}
		}else {
			if (PubUtil.isNotEmpty(vo.getLineIds())) {
				List<String> orgIds = Arrays.asList(vo.getLineIds());
				if (PubUtil.isNotEmpty(orgIds)) {
					vo.setOrgIds(orgIds);
				}
			}
		}
		if(PubUtil.isEmpty(vo.getOrganIds()) || vo.getOrganIds().length<=0){
			vo.setOrganIds(null);
		}
		vo.setCorpId(this.getCurrentUser().getUser().getCorpId());
		Pagination page = deviceOnlineService.getDeviceOnlineStatus(vo);
		GridPage grid = new GridPage(page);
		return this.getReturnResult(grid);
	}
	
    /**
     * 
     * @param vo
     * @throws Exception
     * @author:Qianjy
     * @email:494758538@qq.com
     * @创建日期:2016-4-12
     * @功能说明：
     */
    @RequestMapping("/getDeviceOnlineDetail")
    @ResponseBody
    public Object getDeviceOnlineDetail(CommonParamsVo vo) throws Exception {
        List<DeviceChartVo> list = deviceOnlineService.getDeviceOnlineDetail(vo);
		return this.getReturnResult(list);
    }
	
	/**
	 * @param vo
	 * @param response
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 * @创建日期:2015年7月27日
	 * @功能说明：到处设备在线excel
	 */
	@RequestMapping("/exportDeviceOLStatusExcel")
	public void exportDeviceOLStatusExcel(CommonParamsVo vo,
		HttpServletResponse response) throws Exception {
		if(PubUtil.isNotEmpty(vo.getCarIds())){
			List<String> orgIds = Arrays.asList(vo.getCarIds());
			if(PubUtil.isNotEmpty(orgIds)){
				vo.setOrgIds(orgIds);
			}
		}else {
			if (PubUtil.isNotEmpty(vo.getLineIds())) {
				List<String> orgIds = Arrays.asList(vo.getLineIds());
				if (PubUtil.isNotEmpty(orgIds)) {
					vo.setOrgIds(orgIds);
				}
			}
		}
		if(PubUtil.isEmpty(vo.getOrganIds()) || vo.getOrganIds().length<=0){
			vo.setOrganIds(null);
		}
		vo.setCorpId(this.getCurrentUser().getUser().getCorpId());
		List<BsDayCarOnlineInfoEntity> list = deviceOnlineService.getDeviceOlInfo(vo);
		ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", URLEncoder.encode("设备在线历史明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "设备在线历史明细");
	}
	
	/**
	 * @param vo
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 * @创建日期:2015年7月27日
	 * @功能说明：到处设备在线pdf
	 */
	@RequestMapping("/exportDeviceOLStatusPdf")
	public void exportDeviceOLStatusPdf(CommonParamsVo vo, HttpServletRequest request, 
		HttpServletResponse response) throws Exception {
		if(PubUtil.isNotEmpty(vo.getCarIds())){
			List<String> orgIds = Arrays.asList(vo.getCarIds());
			if(PubUtil.isNotEmpty(orgIds)){
				vo.setOrgIds(orgIds);
			}
		}else {
			if (PubUtil.isNotEmpty(vo.getLineIds())) {
				List<String> orgIds = Arrays.asList(vo.getLineIds());
				if (PubUtil.isNotEmpty(orgIds)) {
					vo.setOrgIds(orgIds);
				}
			}
		}
		if(PubUtil.isEmpty(vo.getOrganIds()) || vo.getOrganIds().length<=0){
			vo.setOrganIds(null);
		}
		vo.setCorpId(this.getCurrentUser().getUser().getCorpId());
		List<BsDayCarOnlineInfoEntity> list = deviceOnlineService.getDeviceOlInfo(vo);
		ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", URLEncoder.encode("设备在线历史明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "设备在线历史明细");
	}

    /**
     * @param vo
     * @param response
     * @throws Exception
     * @author:zhangy
     * @email:zhangyu2222z@sina.cn
     * @创建日期:2015年7月27日
     * @功能说明：到处设备在线excel
     */
    @RequestMapping("/exportOnlineAnalysis")
    public void exportOnlineAnalysis(CommonParamsVo vo,HttpServletResponse response) throws Exception {
        List<BsDayCarOnlineInfoEntity> list = deviceOnlineService.exportOnlineAnalysis(vo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames2), Arrays.asList(tableHeaderFields2), list, response, "excel", URLEncoder.encode("设备在线历史明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "设备在线历史明细");
    }
    
    /**
     * 
     * @param carId
     * @param rcrdDate
     * @author Chenl
     * @email:370238764@qq.com
     * @创建日期:2016年11月14日
     * @功能说明:设备离线率明细
     */
    @RequestMapping("/offLineRateDetailed")
    @ResponseBody
    public Object offLineRateDetailed(String carId,String rcrdDate){
    	Pagination page = deviceOnlineService.offLineRateDetailed(carId, rcrdDate);
		GridPage grid = new GridPage(page);
		return this.getReturnResult(grid);
    }
}
